var loginUsername = "${loginUsername}"; //获得当前登录人员的userName
var operationTaskUid = '${request.getParameter("operationTaskUid")}';
var partTaskUid = '${request.getParameter("partTaskUid")}';

var date = new Date();
var warehouseId;
var loginUser;

//@Bind #view.onReady
!function(self,arg) {
	loginUser = view.id("dsUser").getData("#");
	
}

//@Bind @dtIOEmp.onDataChange
!function(self,arg,dsIOEmp,dsInvIO) {
	var ioEmp = dsIOEmp.getData("#");
	dsIOEmp.set("parameter", {
		cardId : ioEmp.get("cardId")
	}).flush();

	var ioEmp = dsIOEmp.getData("#");
	if (ioEmp){
		var invIO = dsInvIO.insert();
		// alert(curEntity.get("dept").get("deptName"));
		invIO.set("ioType", 24);
		invIO.set("ioTypeDesc", '领用出库');
		invIO.set("ioState", 1);
		invIO.set("ioStateDesc", '草稿');	
		invIO.set("ioDate", date);
		invIO.set("deptUid", ioEmp.get("deptUid"));
		invIO.set("dept.deptName", ioEmp.get("dept.deptName"));
		invIO.set("ioEmployeeUid", ioEmp.get("employeeUid"));
		invIO.set("ioEmployee.employeeName", ioEmp.get("employeeName"));
		invIO.set("creatorUid", ioEmp.get("employeeUid"));
		invIO.set("creator.employeeName", ioEmp.get("employeeName"));
		invIO.set("createTime", date);
		invIO.set("warehouseUid", warehouseId);
		
		if (operationTaskUid){
			invIO.set("taskUid", operationTaskUid);
		}
		if (partTaskUid){
			invIO.set("taskUid", partTaskUid);
		}
		
		var actionGetInvOutBillNumber = view.id("actionGetInvOutBillNumber");
		actionGetInvOutBillNumber.execute(function(result){
			invIO.set("invIOId", result);
		});
	}else {
		dorado.MessageBox.alert("请输入正确的编号...");
	}
	
}

//@Bind #btnAddInvIo.onClick
!function(self,arg) {
	view.id("dsInvIO").clear();
	view.id("dsInvIODetail").clear();
	view.id("dsIOEmp").clear();
	
	if (operationTaskUid || partTaskUid){
		var invIo = view.id("dsInvIO").getData().insert();
		invIo.set("ioType", 24);
		invIo.set("ioTypeDesc", '领用出库');
		invIo.set("ioState", 1);
		invIo.set("ioStateDesc", '草稿');
		invIo.set("ioDate", date);
		invIo.set("createTime", date);
		invIo.set("warehouseUid", warehouseId);
		//invIo.set("uniqueId", getUUid());
		invIo.set("creatorUid", loginUser.get("employeeUid"));
		invIo.set("creator.employeeName", loginUser.get("employeeName"));
		
		if (operationTaskUid){
			invIo.set("taskUid", operationTaskUid);
		}
		if (partTaskUid){
			invIo.set("taskUid", partTaskUid);
		}
		
		var actionGetInvOutBillNumber = view.id("actionGetInvOutBillNumber");
		actionGetInvOutBillNumber.execute(function(result){
			invIo.set("invIOId", result);
		});
	}else {
		dorado.MessageBox.alert("请在左侧选择一个工序或一个零件");
	}
	
}

//@Bind #btnDelInvIo.onClick
!function(self,arg) {
	view.id("dsInvIO").getData("#").remove();
	view.id("dsInvIODetail").getData().each(function(entity){
		entity.remove();
	});
	
}

//@Bind #btnDelInvIoDetail.onClick
!function(self,arg) {
	view.id("dsInvIODetail").getData("#").remove();
	
}

//@Bind #deptTree.onDataRowClick
!function(self, arg, dsInvIO) {
	var invIo = dsInvIO.getData("#");
	var curEntity = self.get("currentEntity");
	
	if (curEntity.dataType.get("name") == "dtEmployee") {
		invIo.set("deptUid", curEntity.get("deptUid"));
		invIo.set("dept.deptName", curEntity.get("dept").get("deptName"));
		invIo.set("ioEmployeeUid", curEntity.get("employeeUid"));
		invIo.set("ioEmployee.employeeName", curEntity.get("employeeName"));
		
		view.id('formEmployeeName').refreshData();
		view.id('formDeptName').refreshData();
	}
	dorado.widget.DropDown.findDropDown(self).close();
	
}

//@Bind #dpIoType.beforeExecute
!function(self,arg,dsTypesData) {
	dsTypesData.set("parameter", {typeName:"iotype"});
	dsTypesData.flush();
}

//@Bind #dgInvInfo.onDataRowClick
!function(self,arg,dsInvIODetail,dsInvInfo) {
	var invInfo = dsInvInfo.getData("#");
	var invIODetail = dsInvIODetail.getData("#");
	
	invIODetail.set("partDrawingId", invInfo.get("partDrawingId"));
	invIODetail.set("partDescription", invInfo.get("partDescription"));
	invIODetail.set("partName", invInfo.get("partName"));
	invIODetail.set("partUid", invInfo.get("partUid"));
	invIODetail.set("partVersionId", invInfo.get("partVersionId"));
	invIODetail.set("batchnum", invInfo.get("batchNum"));
	invIODetail.set("sn", invInfo.get("sn"));
	invIODetail.set("warebinUid", invInfo.get("wareBinUid"));
	invIODetail.set("materialUid", invInfo.get("materialUid"));
	invIODetail.set("ioQty", invInfo.get("invQty"));
	
}

var taskParts;
//@Bind #dgOperationTask.onDataRowClick
!function(self,arg,dsOperationTask) {
	var operationTask = dsOperationTask.getData("#");
	
	taskParts = operationTask.get("taskParts").toArray();

}

//@Bind @dtInvIODetail.onInsert
!function(self, arg, dsInvIODetail, dsInvIO) {
	var invIO = dsInvIO.getData("#");
	var invIODetail = arg.entity;

	invIODetail.set("invIOUid", invIO.get("uniqueId"));
}

// @Bind @dtInvIO.onInsert
!function(self, arg, dsInvIODetail, dsInvIO) {
	var invIO = arg.entity;
	invIO.set("uniqueId", uuid());
	
}

//@Bind #dpWarehouse.onValueSelect
!function(self, arg, dsWarehouse) {
	var warehouse = dsWarehouse.getData("#");
	
	warehouseId = warehouse.get("uuid");
}

//@Bind #btnQuery.onClick
!function(self, arg, afInvInfo, dsInvInfo) {
	var entity = afInvInfo.get("entity");
	entity.set("warehouseUid", warehouseId);

	dsInvInfo.set("parameter",entity).flushAsync();
}

//@Bind #btnQueryTask.onClick
!function(self, arg, afTask, dsOperationTask) {
	var entity = afTask.get("entity");
	dsOperationTask.set("parameter",entity).flushAsync();
}

//@Bind #dsInvInfo.onLoadData
!function(self, arg) {
	self.set("parameter", {
		warehouseUid : warehouseId
	});
}

//@Bind #btnAutoAdd.onClick
!function(self, arg, dsInvIODetail, dsTaskPart, dsInvInfo) {
	dsInvIODetail.clear();
	
	if (operationTaskUid){
		dsTaskPart.set("parameter", {
			taskUid : operationTaskUid
		}).flush();
		
	}
	
	if (partTaskUid){
		dsTaskPart.set("parameter", {
			taskUid : partTaskUid
		}).flush();
	}
	
	if (warehouseId) {
		var taskParts = dsTaskPart.getData().toArray();
		var invIODetail;
		
		for (var i = 0; i < taskParts.length; i++){
			invIODetail = dsInvIODetail.insert();
			invIODetail.set("partDrawingId", taskParts[i].get("part.drawingId"));
			invIODetail.set("partName", taskParts[i].get("part.partName"));
			invIODetail.set("partDescription", taskParts[i].get("part.description"));
			invIODetail.set("applyqty", taskParts[i].get("reqQty"));

			dsInvInfo.set("parameter", {
				partDrawingId : taskParts[i].get("part.drawingId"),
				warehouseUid : warehouseId
			}).flush();
			
			var invInfos = dsInvInfo.getData().toArray();
			var curReqQty = invIODetail.get("applyqty");
			
			for (var j = 0; j < invInfos.length; j++){
				//var curReqQty = reqQty;
				if (curReqQty < invInfos[j].get("invQty")){
					invIODetail.set("batchnum", invInfos[j].get("batchNum"));
					invIODetail.set("sn", invInfos[j].get("sn"));
					invIODetail.set("materialUid", invInfos[j].get("materialUid"));
					invIODetail.set("warebinUid", invInfos[j].get("warebinUid"));
					invIODetail.set("partUid", invInfos[j].get("partUid"));
					invIODetail.set("partVersionId", invInfos[j].get("partVersionId"));
					view.id("FormwarebinUid").refreshData();
					invIODetail.set("warebin.binName", invInfos[j].get("warebin.binName"));
					invIODetail.set("ioQty", curReqQty);
					
					break;
				}else if (invInfos[j].get("invQty") > 0) {
					invIODetail.set("batchnum", invInfos[j].get("batchNum"));
					invIODetail.set("sn", invInfos[j].get("sn"));
					invIODetail.set("partUid", invInfos[j].get("partUid"));
					invIODetail.set("partVersionId", invInfos[j].get("partVersionId"));
					invIODetail.set("warebinUid", invInfos[j].get("warebinUid"));
					view.id("FormwarebinUid").refreshData();
					invIODetail.set("warebin.binName", invInfos[j].get("warebin.binName"));
					invIODetail.set("materialUid", invInfos[j].get("materialUid"));
					invIODetail.set("ioQty", invInfos[j].get("invQty"));
					
					curReqQty = curReqQty - invInfos[j].get("invQty");
					
					if (j < invInfos.length - 1){
						invIODetail = dsInvIODetail.insert();
						invIODetail.set("partDrawingId", taskParts[i].get("part.drawingId"));
						invIODetail.set("partName", taskParts[i].get("part.partName"));
						invIODetail.set("partDescription", taskParts[i].get("part.description"));
						invIODetail.set("applyqty", 0);
					}
				}
			}
			
		}
	}else {
		dorado.MessageBox.alert("请选择出库库房");
	}
	
	
	
	
}

//@Bind #dgInvIODetail.onDataRowClick
!function(self, arg, dsInvInfo, dsInvIODetail) {
	var invIODetail = dsInvIODetail.getData("#");
	
	dsInvInfo.set("parameter", {
		partDrawingId : invIODetail.get("partDrawingId"),
		warehouseUid : warehouseId
	});
	dsInvInfo.flush();
	
}

//@Bind #dgInfo.onDataRowClick
!function(self, arg, dsInvIODetail, dsInvInfo) {
	var invIODetail = dsInvIODetail.getData("#");
	var invInfo = dsInvInfo.getData("#");
	
	invIODetail.set("batchnum", invInfo.get("batchNum"));
	invIODetail.set("sn", invInfo.get("sn"));
	invIODetail.set("warebinUid", invInfo.get("warebinUid"));
	invIODetail.set("materialUid", invInfo.get("materialUid"));
	
	var invQty = invInfo.get("invQty");
	var applyqty = invIODetail.get("applyqty");
	invIODetail.set("ioQty", applyqty);
	
}

//@Bind #saveRecord.onSuccess
!function(self, arg) {
	var dsInvIO = view.id("dsInvIO");
	var invIO = dsInvIO.getData("#");

	invIO.set("ioState",5);
	invIO.set("ioStateDesc","已登账");
	
}

//@Bind #dgInvIODetail.onCellValueEdit
!function(self, arg, dsInvInfo) {
	if (arg.column.get("name") == "ioQty"){
		var invInfo = dsInvInfo.getData("#");
		var oldQty = invInfo.get("invQty");
		var newQty = arg.value;
		if (oldQty < newQty){
			dorado.MessageBox.alert("库存数量不足，请重新输入...");
			self.get("currentEntity").set("ioQty", oldQty);
		}
	}
}

//生成uuid
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
 
    var uuid = s.join("");
    return uuid;
}

